
//
//  ChangeVerifyView.m
//  HYApp
//
//  Created by Jianbo Cai on 2020/7/24.
//  Copyright © 2020 Unidax. All rights reserved.
//

#import "ChangeVerifyView.h"
#import "HYNetHelper.h"
#import "AWUserInfo.h"

@interface ChangeVerifyView ()

@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UIButton *changeBtn;

@property (nonatomic, assign) NSInteger type;

@end

@implementation ChangeVerifyView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    WEAKSELF
    self.codeText.block = ^{
        [weakSelf get2faCode];
    };
    
    NSArray *available2faMethods = User().available2faMethods;
    if(available2faMethods && [available2faMethods isKindOfClass:[NSArray class]]){
        if (available2faMethods.count == 2) {//手机登录
            self.verificationType = VerificationTypeWithALL;
        }else if ([available2faMethods containsObject:@"sms"]) {//手机登录
            self.verificationType = VerificationTypeWithOnlySMS;
        }else{//google验证
            self.verificationType = VerificationTypeWithOnlyGA;
        }
    }else {
        self.verificationType =  VerificationTypeWithALL;
    }
}

- (void)get2faCode {
    if(self.isLogin){
        [HYNetHelper smsAuthorizationCode:self.accessToken appId:[HYNetHelper appid] callBack:^(JFResultVo *resultVo) {
            if (resultVo.errorNo == 0) {
                
            }
        }];
    }else{
        [HYNetHelper get2faCode:^(JFResultVo *resultVo) {
            if(resultVo.errorNo == 0){
                
            }
        }];
    }
}

- (void)setType:(NSInteger)type {
    _type = type;
    
    if (type == 0) {
        self.title.text = [JFMainLocalizedString(@"GA code") stringByReplacingOccurrencesOfString:@" " withString:@""];
        self.codeText.rightView = nil;
        [self.changeBtn setTitle:JFMainLocalizedString(@"SMS Verity") forState:UIControlStateNormal];
    }else {
        self.title.text = [JFMainLocalizedString(@"SMS Verity") stringByReplacingOccurrencesOfString:@" " withString:@""];
        [self.codeText addSendButton];
        [self.changeBtn setTitle:JFMainLocalizedString(@"GA code") forState:UIControlStateNormal];
    }
}

- (void)setVerificationType:(VerificationType)verificationType {
    _verificationType = verificationType;
    
    switch (verificationType) {
        case VerificationTypeWithOnlySMS: {
            self.type = 1;
            self.changeBtn.hidden = YES;
        }
            break;
        case VerificationTypeWithOnlyGA: {
            self.type = 0;
            self.changeBtn.hidden = YES;
        }
            break;
        case VerificationTypeWithALL: {
            self.type = 0;
            self.changeBtn.hidden = NO;
        }
            break;
        default:
            break;
    }
}

- (IBAction)changeAction:(UIButton *)sender {
    self.type = !self.type;
}

-(NSString*)verifyType{
    if (self.type == 0) {
        return @"1";
    }else if(self.type == 1){
        return @"2";
    }else{
        return @"0";
    }
}
@end
